import { request } from '../utils/request.js'
/**
 * 微信登录获取code
 */
export function wxLogin() {
  return new Promise((resolve, reject) => {
    wx.login({
      timeout: 10000,
      success: (result) => {
        resolve(result);
      },
      fail: () => {
        reject(1);
      }
    });
  });
}
/**
 * 用户授权
 */
export function authUser(paramObj) {
  let param;
  if (paramObj.userInfo) {
    param = {
      code: paramObj.code,
      userInfo: paramObj.userInfo,
    };
  } else {
    param = {
      code: paramObj.code,
      encryptedData: paramObj.encryptedData,
      iv: paramObj.iv
    };
  }
  return request('wxScan/oauth_user6', 'POST', param);
}

// 静默授权
export function authUserSilence(code) {
  return request('wxScan/oauth_user7', 'POST', { code });
}
/**
 * 获取用户信息
 * 
 */
export function wxGetUserInfo() {
  return new Promise((resolve, reject) => {
    wx.getUserInfo({
      lang: 'zh_CN',
      success: (result) => {
        resolve(result);
      },
      fail: () => {
        reject(1);
      },
    });
  });
}

export function wxGetUserInfoNew() {
  return new Promise((resolve, reject) => {
    wx.getUserProfile({
      lang: 'zh_CN',
      desc: '业务需要',
      success: (result) => {
        resolve(result);
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
}
